#include "addfriend.h"
#include "ui_addfriend.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QTcpSocket>
#include <QMouseEvent>
#define fLabeltext(name) textlbl##name  //动态命名变量名
#define fbtn(name) f_btn##name
#define hlayout(name) hlayout##name
#define addbtn(name) add_btn##name

addFriend::addFriend(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::addFriend)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::FramelessWindowHint);

    if(ui->lineEdit->text().isEmpty()){
        ui->lineEdit->setPlaceholderText(tr("请输入账号"));
    }
    else{
        ui->lineEdit->setPlaceholderText(QString::null);
    }

    ui->lineEdit->setStyleSheet("background:transparent;border-width:0;border-top:1px  solid;border-bottom:1px  solid;border-left:1px  solid;border-right: 1px  solid;border-style:outset");
    ui->add_btn->hide();

    tcpsocket=new QTcpSocket(this);
    tcpsocket->connectToHost("localhost",8022);
    connect(tcpsocket, &QTcpSocket::connected, this, &addFriend::onConnected);
    connect(tcpsocket, &QTcpSocket::disconnected, this, &addFriend::onDisconnected);
    connect(tcpsocket, &QTcpSocket::readyRead, this, &addFriend::onReadyRead);
}

addFriend::~addFriend()
{
    delete ui;
}

//点击查询好友按钮
void addFriend::on_search_btn_clicked()
{
    QJsonObject dataObject;
    dataObject["sendtype"]="searchfriend";
    dataObject["selfacc"]=selfaccount;
    dataObject["searchacc"]=ui->lineEdit->text();
    QJsonDocument dataDocument(dataObject);
    QByteArray jsonData=dataDocument.toJson();
    tcpsocket->write(jsonData);
    num=0;
}

void addFriend::getselfaccount(const QString &a){
    selfaccount=a;
}

//接收服务器端发送的消息
void addFriend::onReadyRead(){
    QByteArray jsonData=tcpsocket->readAll();
    QJsonDocument dataDocument=QJsonDocument::fromJson(jsonData);
    qDebug()<<(!dataDocument.isNull()&&dataDocument.isObject())<<endl;

    if(!dataDocument.isNull()&&dataDocument.isObject()){
        QJsonObject dataObject=dataDocument.object();
        QJsonObject dataObject1;
        if(dataObject["returntype"].toString()=="addfriend"&&num==0){
          emit addsuccess(fri_pic,fri_name,selfaccount,fri_account);
             num=1;
        }

        else if(dataObject["returntype"].toString()=="searchfriend"&&selfaccount==dataObject["selfaccount"].toString()){
                 if(dataObject["searchresult"].toBool()){
                fri_pic=dataObject["searchpic"].toString();
                fri_name=dataObject["searchname"].toString();
                QPixmap*pix=new QPixmap(fri_pic);
                ui->pic->setAlignment(Qt::AlignCenter);
                ui->pic->setPixmap(pix->scaled(ui->label->width(),ui->label->height(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
                ui->label->setText(fri_name+"("+ui->lineEdit->text()+")");
                ui->add_btn->show();
            }
               else{
                QMessageBox::information(this,tr("提示"),tr("所搜用户不存在"),QMessageBox::Ok);
            }
         }
      }
}

void addFriend::onConnected(){
    qDebug()<<"addfriend onConnected"<<endl;
}

void addFriend::onDisconnected(){
    qDebug()<<"addfriend onDisconnected"<<endl;
}

void addFriend::on_add_btn_clicked()
{
    QJsonObject dataObject;
    dataObject["sendtype"]="addfriend";
    dataObject["selfaccount"]=selfaccount;
    dataObject["friendinformation"]=ui->lineEdit->text();
    fri_account=ui->lineEdit->text();

    QJsonDocument dataDocument(dataObject);
    QByteArray jsonData=dataDocument.toJson();
    tcpsocket->write(jsonData);
}

QString addFriend::getfriendpic(){
    return fri_pic;
}

QString addFriend::getfriendname(){
    return fri_name;
}
QString addFriend::getfriacc(){
    return fri_account;
}

void addFriend::on_toolButton_clicked()
{
    this->showMinimized();
}

void addFriend::on_toolButton_2_clicked()
{
    this->close();
}

void addFriend::mousePressEvent(QMouseEvent *event)
{
    this->windowPos = this->pos();       // 获得部件当前位置
    this->mousePos = event->globalPos(); // 获得鼠标位置
    this->dPos = mousePos - windowPos;   // 移动后部件所在的位置
}

void addFriend::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - this->dPos);
}
